const fs = require("fs")
const {JSDOM} = require("jsdom")
module.exports = class HtmlPlugin{
  constructor(options){
    this.options = options;
  }

  apply(complier){
    complier.hooks.emit.tap("HtmlPlugin",()=>{
      const filename = complier.options.output.filename;
      const bundlePath = `./${filename}`;

      const htmlCode = fs.readFileSync(this.options.template).toString();
      const dom = new JSDOM(htmlCode);
      const body = dom.window.document.querySelector("body");
      body.innerHTML += `<script src = "${bundlePath}"></script>`;

      complier.assets[this.options.filename] = dom.serialize();
    })
  }
}
